package com.htfyun.roobofactory.factorytest;

import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.htfyun.roobofactory.BaseActivity;
import com.htfyun.roobofactory.R;
import com.roobo.hardware.LedCtrlManager;

/**
 * Created by sst on 2018/7/9.
 */

public class LedTestActivity extends BaseActivity {

    private Button btnMarqueeOn;
    private Button btnMarqueeOff;
    private CheckBox LeftRCkb,LeftGCkb, LeftBCkb, powerCkb;


    private Button btnPass;
    private Button btnFail;

    private LedCtrlManager mLedCtrlManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_factorytest_led);

        mLedCtrlManager = LedCtrlManager.getInstance();

        btnMarqueeOn = (Button) findViewById(R.id.btnMarqueeOn);
        btnMarqueeOff = (Button) findViewById(R.id.btnMarqueeOff);

        btnMarqueeOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.e(TAG, "btnMarqueeOn");

                for (int i = 0; i < mLedCtrlManager.getMaxLedNum(LedCtrlManager.LED_NUM_ALL); i++) {
                    mLedCtrlManager.setLedLight(i, mLedCtrlManager.getMaxLedBrightness(LedCtrlManager.LED_ID_LEFT_BASE), 0);
                }
            }
        });

        btnMarqueeOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.e(TAG, "btnMarqueeOff");
                for (int i = 0; i < mLedCtrlManager.getMaxLedNum(LedCtrlManager.LED_NUM_ALL); i++) {
                    mLedCtrlManager.setLedLight(i, 0, 0);
                }
            }
        });

        LeftRCkb = (CheckBox)findViewById(R.id.checkBoxleftr);
        LeftGCkb = (CheckBox)findViewById(R.id.checkBoxleftg);
        LeftBCkb = (CheckBox)findViewById(R.id.checkBoxleftb);
        powerCkb = (CheckBox) findViewById(R.id.checkBoxLedPower);

        LeftRCkb.setChecked(mLedCtrlManager.getSingleLedRGBRedOn());
        LeftGCkb.setChecked(mLedCtrlManager.getSingleLedRGBGreenOn());
        LeftBCkb.setChecked(mLedCtrlManager.getSingleLedRGBBlueOn());
        powerCkb.setChecked(mLedCtrlManager.getSingleLedPowerOn());

        LeftRCkb.setOnCheckedChangeListener(onCheckedChangeListener);
        LeftGCkb.setOnCheckedChangeListener(onCheckedChangeListener);
        LeftBCkb.setOnCheckedChangeListener(onCheckedChangeListener);
        powerCkb.setOnCheckedChangeListener(onCheckedChangeListener);


        btnPass = (Button) findViewById(R.id.btnPass);
        btnFail = (Button) findViewById(R.id.btnFail);
        btnPass.setOnClickListener(onPassOrFailClickListener);
        btnFail.setOnClickListener(onPassOrFailClickListener);
    }

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener =
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (buttonView == LeftRCkb) {

                        mLedCtrlManager.setSingleLedRGBRedOn(isChecked);

                    } else if (buttonView == LeftGCkb) {

                        mLedCtrlManager.setSingleLedRGBGreenOn(isChecked);

                    } else if (buttonView == LeftBCkb) {

                        mLedCtrlManager.setSingleLedRGBBlueOn(isChecked);

                    } else if (buttonView == powerCkb) {

                        mLedCtrlManager.setSingleLedPowerOn(isChecked);

                    }

                }
            };

    private void setBtnPassOrFailEnabled(boolean enabled) {
        btnFail.setEnabled(enabled);
        btnPass.setEnabled(enabled);
    }

    private View.OnClickListener onPassOrFailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btnPass) {

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());

                int leftNum = mLedCtrlManager.getMaxLedNum(LedCtrlManager.LED_NUM_LEFT);
                int rightNum = mLedCtrlManager.getMaxLedNum(LedCtrlManager.LED_NUM_RIGHT);
                int maxLight = mLedCtrlManager.getMaxLedBrightness(LedCtrlManager.LED_ID_LEFT_BASE);
                int minLight = mLedCtrlManager.getMinLedBrightness(LedCtrlManager.LED_ID_LEFT_BASE);
                String log = (getString(R.string.led_marquee_info_en, leftNum, rightNum, maxLight, minLight));
                FactoryTestItemsEnum.test_led.setLog(getActivity(), log);

            } else if (v == btnFail) {

                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(getActivity());

            }
        }
    };
}
